package com.ruoyi.project.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.domain.InsReagentBox;
import com.ruoyi.project.product.service.IInsReagentBoxService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 试剂盒记录Controller
 *
 * @author taney
 * @date 2020-11-25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/product/reagentBox")
public class InsReagentBoxController extends BaseController {

    private final IInsReagentBoxService iInsReagentBoxService;

    /**
     * 查询试剂盒记录列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBox:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsReagentBox insReagentBox) {
        startPage();
        LambdaQueryWrapper<InsReagentBox> lqw = new LambdaQueryWrapper<InsReagentBox>();
        if (insReagentBox.getOrderListId() != null) {
            lqw.eq(InsReagentBox::getOrderListId, insReagentBox.getOrderListId());
        }
        if (insReagentBox.getOrderId() != null) {
            lqw.eq(InsReagentBox::getOrderId, insReagentBox.getOrderId());
        }
        if (StringUtils.isNotBlank(insReagentBox.getProjectCode())) {
            lqw.eq(InsReagentBox::getProjectCode, insReagentBox.getProjectCode());
        }
        if (StringUtils.isNotBlank(insReagentBox.getProjectName())) {
            lqw.like(InsReagentBox::getProjectName, insReagentBox.getProjectName());
        }
        if (StringUtils.isNotBlank(insReagentBox.getTestMethod())) {
            lqw.eq(InsReagentBox::getTestMethod, insReagentBox.getTestMethod());
        }
        if (StringUtils.isNotBlank(insReagentBox.getItemNo())) {
            lqw.eq(InsReagentBox::getItemNo, insReagentBox.getItemNo());
        }
        if (StringUtils.isNotBlank(insReagentBox.getReagentSpec())) {
            lqw.eq(InsReagentBox::getReagentSpec, insReagentBox.getReagentSpec());
        }
        if (StringUtils.isNotBlank(insReagentBox.getExpireDate())) {
            lqw.eq(InsReagentBox::getExpireDate, insReagentBox.getExpireDate());
        }
        if (StringUtils.isNotBlank(insReagentBox.getLotNo())) {
            lqw.eq(InsReagentBox::getLotNo, insReagentBox.getLotNo());
        }
        if (StringUtils.isNotBlank(insReagentBox.getBarcode())) {
            lqw.eq(InsReagentBox::getBarcode, insReagentBox.getBarcode());
        }
        if (StringUtils.isNotBlank(insReagentBox.getRegistrationNo())) {
            lqw.eq(InsReagentBox::getRegistrationNo, insReagentBox.getRegistrationNo());
        }
        if (StringUtils.isNotBlank(insReagentBox.getProductionLicenseNo())) {
            lqw.eq(InsReagentBox::getProductionLicenseNo, insReagentBox.getProductionLicenseNo());
        }
        if (StringUtils.isNotBlank(insReagentBox.getPrintState())) {
            lqw.eq(InsReagentBox::getPrintState, insReagentBox.getPrintState());
        }
        lqw.orderByDesc(InsReagentBox::getOrderId);
        List<InsReagentBox> list = iInsReagentBoxService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出试剂盒记录列表
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBox:export')")
    @Log(title = "试剂盒记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsReagentBox insReagentBox) {
        LambdaQueryWrapper<InsReagentBox> lqw = new LambdaQueryWrapper<InsReagentBox>(insReagentBox);
        List<InsReagentBox> list = iInsReagentBoxService.list(lqw);
        ExcelUtil<InsReagentBox> util = new ExcelUtil<InsReagentBox>(InsReagentBox.class);
        return util.exportExcel(list, "reagentBox");
    }

    /**
     * 获取试剂盒记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBox:query')")
    @GetMapping(value = "/{boxId}")
    public AjaxResult getInfo(@PathVariable("boxId") Integer boxId) {
        return AjaxResult.success(iInsReagentBoxService.getById(boxId));
    }

    /**
     * 新增试剂盒记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBox:add')")
    @Log(title = "试剂盒记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsReagentBox insReagentBox) {
        return toAjax(iInsReagentBoxService.save(insReagentBox) ? 1 : 0);
    }

    /**
     * 修改试剂盒记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBox:edit')")
    @Log(title = "试剂盒记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsReagentBox insReagentBox) {
        return toAjax(iInsReagentBoxService.updateById(insReagentBox) ? 1 : 0);
    }

    /**
     * 删除试剂盒记录
     */
    @PreAuthorize("@ss.hasPermi('product:reagentBox:remove')")
    @Log(title = "试剂盒记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{boxIds}")
    public AjaxResult remove(@PathVariable Integer[] boxIds) {
        return toAjax(iInsReagentBoxService.removeByIds(Arrays.asList(boxIds)) ? 1 : 0);
    }

}
